#include "PCResource_theme_default.h"

enum Module
{
  CPU = 0,
  GPU,
  RAM,
  SYS,
  OTHERS,
};

enum ModuleCPU
{
  CPU_TEMP = 0,
  CPU_CLOCK,
  CPU_POWER,
  CPU_FAN_SPEED
};

enum ModuleGPU
{
  GPU_TEMP = 0,
  GPU_GRAM_USED,
  GPU_POWER,
  GPU_FAN_SPEED
};

enum ModuleRAM
{
  RAM_USED = 0,
};

enum ModuleSys
{
  SYS_DISK = 0,
  SYS_NET_SPEED,
};

enum ModuleOthers
{
  OTHERS_DATE = 0,
  OTHERS_TIME,
};

LV_FONT_DECLARE(lv_font_mh_72);
LV_FONT_DECLARE(lv_font_mh_24);
LV_FONT_DECLARE(lv_font_mh_20);

LV_IMG_DECLARE(cpu_icon);
LV_IMG_DECLARE(gpu_icon);
LV_IMG_DECLARE(ram_icon);
LV_IMG_DECLARE(technology);

static void pc_resource_gui_default_theme_init(lv_obj_t *parent, gui_opt_t *opts);
static void pc_resource_gui_default_theme_update(PCResInfo *info);
static void pc_resource_gui_default_theme_release(void);
static void pc_resource_gui_default_theme_icon_event_cb(lv_event_t *e);

gui_theme_operations default_theme = 
{
    .init = pc_resource_gui_default_theme_init,
    .update = pc_resource_gui_default_theme_update,
    .release = pc_resource_gui_default_theme_release,
};

// 回调函数集合
static gui_opt_t *gui_opts;

//纯色背景
static lv_obj_t *background;

//组件容器
static lv_obj_t *module_cont[5];

//组件名称label
static lv_obj_t *module_label[5];

//3大组件arc
static lv_obj_t *module_arc[3];

//3大组件arc对应数值label
static lv_obj_t *module_arc_label[3];

//图标
static lv_obj_t *icon;

//组件的扩展label
static lv_obj_t *module_ext_label[5][4];

//组件扩展label数量
static const int module_ext_num[5] = 
{
  4, 4, 1, 2, 2
};

//3大组件arc上部icon img
static const lv_img_dsc_t *arc_img[] = 
{
  &cpu_icon, &gpu_icon, &ram_icon
};

//5大组件容器尺寸
static const lv_coord_t cont_size[5][2] = 
{
    {497, 285}, {497, 285}, {328, 285}, {328, 285}, {328, 285}
};

//5大组件名称
static const char *module_name[5] = 
{
  "处理器", "显卡", "内存", "系统", "其他"
};

//5大组件扩展label名称
static const char *module_ext_name[][5] = {
  {"    温度    ", "    主频    ", "    功耗    ", "风扇转速 ", ""},
  {"    温度    ", "已用显存 ", "    功耗    ", "风扇转速 ", ""},
  {"已使用 ", "", "", "", ""},
  {"系统盘", "网络速率", "", "", ""},
  {"日期    ", "时间    ", "", "", ""}
};

//5大组件扩展label数值
static const char *module_ext_value[][5] = {
  {"00℃", "0000MHz", "00.0W", "0000RPM", ""},
  {"00℃", "0000MB", "00.0W", "0000RPM", ""},
  {"0000MB", "", "", "", ""},
  {"\n温度 00℃  使用率 00%", "\n0000.0KB/s\n0000.0KB/s", "", "", ""},
  {"1970-01-01", "00:00", "", "", ""}
};

/*
  *@brief 默认主题初始化函数
  */
static void pc_resource_gui_default_theme_init(lv_obj_t *parent, gui_opt_t *opts)
{
    gui_opts = opts;

    //创建纯色背景
    background = lv_obj_create(parent);
    lv_obj_clear_flag(background, LV_OBJ_FLAG_SCROLLABLE);        //禁止对象滚动
    lv_obj_set_size(background, 1024, 600);
    lv_obj_center(background);
    lv_obj_set_style_bg_color(background, lv_color_hex(0xff393b47) , LV_STATE_DEFAULT);
    lv_obj_set_style_border_width(background,0, LV_STATE_DEFAULT);      //边框宽度0
    lv_obj_set_style_pad_all(background, 0, LV_STATE_DEFAULT);      //四周填充宽度0
    lv_obj_set_style_outline_width(background, 0, LV_STATE_DEFAULT);        //外部线条宽度0
    lv_obj_set_style_radius(background, 0, LV_STATE_DEFAULT);       //边框圆角半径0


    //创建组件容器和相关元素
    for(int i = 0; i < 5; i++)
    {
        lv_obj_t *cont = lv_obj_create(background);
        lv_obj_set_size(cont, cont_size[i][0], cont_size[i][1]);
        lv_obj_clear_flag(cont, LV_OBJ_FLAG_SCROLLABLE);        //禁止对象滚动
        lv_obj_set_style_bg_opa(cont, LV_OPA_TRANSP, LV_STATE_DEFAULT);     //背景透明
        lv_obj_set_style_border_opa(cont, LV_OPA_70, LV_STATE_DEFAULT);     //边框透明度
        module_cont[i] = cont;

        //组件标题
        lv_obj_t *label = lv_label_create(cont);
        lv_obj_set_style_text_font(label, &lv_font_mh_24, LV_STATE_DEFAULT);           //设置字体
        lv_obj_set_style_text_opa(label, LV_OPA_90, LV_STATE_DEFAULT);       //字体透明度
        lv_obj_set_style_text_color(label, lv_color_hex(0x6bbfff), LV_STATE_DEFAULT);       //字体颜色
        lv_label_set_text(label, module_name[i]);
        lv_obj_align(label, LV_ALIGN_TOP_LEFT, -10, -20);
        module_label[i] = label;

        for(int j = 0; j < module_ext_num[i]; j++)
        {
          label = lv_label_create(cont);
          lv_obj_set_style_text_font(label, &lv_font_mh_20, LV_STATE_DEFAULT);           //设置字体
          lv_obj_set_style_text_opa(label, LV_OPA_90, LV_STATE_DEFAULT);       //字体透明度
          lv_obj_set_style_text_color(label, lv_color_white(), LV_STATE_DEFAULT);       //字体颜色
          lv_label_set_text_fmt(label, "%s%s", module_ext_name[i][j], module_ext_value[i][j]);    //设置文本

          module_ext_label[i][j] = label;
        }

        if(i >= 3)
            continue;

        //使用率弧形小工具
        lv_obj_t *arc = lv_arc_create(cont);
        lv_obj_set_size(arc, 220, 220);
        lv_obj_center(arc);
        lv_obj_remove_style(arc, NULL, LV_PART_KNOB);       //移除旋钮部件
        lv_obj_clear_flag(arc, LV_OBJ_FLAG_CLICKABLE);      //不可点击
        lv_arc_set_rotation(arc, 270);
        lv_arc_set_range(arc, 0, 100);
        lv_arc_set_bg_angles(arc, 0, 360);
        
        lv_arc_set_value(arc, 50);        
        lv_obj_set_style_arc_width(arc, 20, LV_PART_MAIN | LV_STATE_DEFAULT);      //设置圆弧宽度
        lv_obj_set_style_arc_width(arc, 20, LV_PART_INDICATOR | LV_STATE_DEFAULT);      //设置圆弧指示器宽度
        module_arc[i] = arc;
        
        //使用率弧形小工具数值标签
        label = lv_label_create(arc);
        lv_obj_set_style_text_font(label, &lv_font_mh_72, LV_STATE_DEFAULT);           //设置字体
        lv_obj_set_style_text_opa(label, LV_OPA_90, LV_STATE_DEFAULT);       //字体透明度
        lv_obj_set_style_text_color(label, lv_color_white(), LV_STATE_DEFAULT);       //字体颜色
        lv_label_set_text_fmt(label, "%d", 50);
        lv_obj_center(label);
        module_arc_label[i] = label;

        //使用率%文本标签
        label = lv_label_create(arc);
        lv_obj_set_style_text_font(label, &lv_font_mh_20, LV_STATE_DEFAULT);           //设置字体
        lv_obj_set_style_text_opa(label, LV_OPA_90, LV_STATE_DEFAULT);       //字体透明度
        lv_obj_set_style_text_color(label, lv_color_white(), LV_STATE_DEFAULT);       //字体颜色
        lv_label_set_text(label, "使用率%");
        lv_obj_align(label, LV_ALIGN_BOTTOM_MID, 0, -40);

        //创建arc上部icon img
        lv_obj_t *img = lv_img_create(arc);
        lv_img_set_src(img, arc_img[i]);
        lv_img_set_zoom(img, 384);
        lv_obj_align(img, LV_ALIGN_TOP_MID, 0, 30);
    }

    //创建图标
    icon = lv_img_create(module_cont[OTHERS]);
    lv_img_set_src(icon, &technology);
    lv_obj_align(icon, LV_ALIGN_BOTTOM_MID, 0, 0); 
    lv_obj_add_flag(icon, LV_OBJ_FLAG_CLICKABLE);    //设置可点击
    lv_obj_add_event_cb(icon, pc_resource_gui_default_theme_icon_event_cb, LV_EVENT_SHORT_CLICKED, NULL);
    if (gui_opts != NULL && gui_opts->exit_cb != NULL)
      lv_obj_add_event_cb(icon, gui_opts->exit_cb, LV_EVENT_LONG_PRESSED, NULL);

    //调整组件容器位置对齐
    lv_obj_set_pos(module_cont[CPU], 10, 10);
    lv_obj_align_to(module_cont[GPU], module_cont[CPU], LV_ALIGN_OUT_RIGHT_MID, 10, 0);
    lv_obj_align_to(module_cont[RAM], module_cont[CPU], LV_ALIGN_OUT_BOTTOM_LEFT, 0, 10);
    lv_obj_align_to(module_cont[SYS], module_cont[RAM], LV_ALIGN_OUT_RIGHT_MID, 10, 0);
    lv_obj_align_to(module_cont[OTHERS], module_cont[SYS], LV_ALIGN_OUT_RIGHT_MID, 10, 0);

    //调整组件arc对齐
    lv_obj_align(module_arc[CPU], LV_ALIGN_BOTTOM_LEFT, -10, 10); 
    lv_obj_align(module_arc[GPU], LV_ALIGN_BOTTOM_LEFT, -10, 10);
    lv_obj_align(module_arc[RAM], LV_ALIGN_BOTTOM_MID, 0, 10);

    //调整组件ext label对齐
    lv_obj_align_to(module_ext_label[CPU][CPU_TEMP], module_arc[CPU], LV_ALIGN_OUT_RIGHT_TOP, 10, 0);
    lv_obj_align_to(module_ext_label[CPU][CPU_CLOCK], module_ext_label[CPU][CPU_TEMP], LV_ALIGN_OUT_BOTTOM_LEFT, 0, 0);
    lv_obj_align_to(module_ext_label[CPU][CPU_POWER], module_ext_label[CPU][CPU_CLOCK], LV_ALIGN_OUT_BOTTOM_LEFT, 0, 0);
    lv_obj_align_to(module_ext_label[CPU][CPU_FAN_SPEED], module_ext_label[CPU][CPU_POWER], LV_ALIGN_OUT_BOTTOM_LEFT, 0, 0);

    lv_obj_align_to(module_ext_label[GPU][GPU_TEMP], module_arc[GPU], LV_ALIGN_OUT_RIGHT_TOP, 10, 0);
    lv_obj_align_to(module_ext_label[GPU][GPU_POWER], module_ext_label[GPU][GPU_TEMP], LV_ALIGN_OUT_BOTTOM_LEFT, 0, 0);
    lv_obj_align_to(module_ext_label[GPU][GPU_GRAM_USED], module_ext_label[GPU][GPU_POWER], LV_ALIGN_OUT_BOTTOM_LEFT, 0, 0);
    lv_obj_align_to(module_ext_label[GPU][GPU_FAN_SPEED], module_ext_label[GPU][GPU_GRAM_USED], LV_ALIGN_OUT_BOTTOM_LEFT, 0, 0);

    lv_obj_align_to(module_ext_label[RAM][RAM_USED], module_label[RAM], LV_ALIGN_OUT_RIGHT_BOTTOM, 40, 0);

    lv_obj_align_to(module_ext_label[SYS][SYS_DISK], module_label[SYS], LV_ALIGN_OUT_BOTTOM_LEFT, 10, 0);
    lv_obj_align_to(module_ext_label[SYS][SYS_NET_SPEED], module_ext_label[SYS][SYS_DISK], LV_ALIGN_OUT_BOTTOM_LEFT, 0, 0);

    lv_obj_align_to(module_ext_label[OTHERS][OTHERS_DATE], module_label[OTHERS], LV_ALIGN_OUT_BOTTOM_LEFT, 10, 0);
    lv_obj_align_to(module_ext_label[OTHERS][OTHERS_TIME], module_ext_label[OTHERS][OTHERS_DATE], LV_ALIGN_OUT_BOTTOM_LEFT, 0, 0);
}

/*
  *@brief 默认主题数据更新函数
  */
static void pc_resource_gui_default_theme_update(PCResInfo *info)
{
    if(info == NULL)
      return;

    lv_anim_t anim;
    lv_anim_init(&anim);
    lv_anim_set_exec_cb(&anim, (lv_anim_exec_xcb_t)lv_arc_set_value);
    lv_anim_set_time(&anim, 500);     //动画时间500ms

    //设置CPU组件
    //lv_arc_set_value(module_arc[CPU], info->cpu_usage);       //静态设置
    lv_anim_set_var(&anim, module_arc[CPU]);
    lv_anim_set_values(&anim, lv_arc_get_value(module_arc[CPU]), info->cpu_usage);
    lv_anim_start(&anim);     //添加线性动画

    lv_label_set_text_fmt(module_arc_label[CPU], "%d", info->cpu_usage);
    lv_label_set_text_fmt(module_label[CPU], "%s %s", module_name[CPU], info->cpu_name);
    lv_label_set_text_fmt(module_ext_label[CPU][CPU_TEMP], "%s%d℃", module_ext_name[CPU][CPU_TEMP], info->cpu_temp);
    lv_label_set_text_fmt(module_ext_label[CPU][CPU_CLOCK], "%s%dMHz", module_ext_name[CPU][CPU_CLOCK], info->cpu_freq);
    lv_label_set_text_fmt(module_ext_label[CPU][CPU_POWER], "%s%d.%dW", module_ext_name[CPU][CPU_POWER], info->cpu_power / 10, info->cpu_power % 10);
    lv_label_set_text_fmt(module_ext_label[CPU][CPU_FAN_SPEED], "%s%dRPM", module_ext_name[CPU][CPU_FAN_SPEED], info->cpu_fan_speed);

    //设置GPU组件
    //lv_arc_set_value(module_arc[GPU], info->gpu_usage);     //静态设置
    lv_anim_set_var(&anim, module_arc[GPU]);
    lv_anim_set_values(&anim, lv_arc_get_value(module_arc[GPU]), info->gpu_usage);
    lv_anim_start(&anim);   //添加动画

    lv_label_set_text_fmt(module_arc_label[GPU], "%d", info->gpu_usage);
    lv_label_set_text_fmt(module_label[GPU], "%s %s", module_name[GPU], info->gpu_name);
    lv_label_set_text_fmt(module_ext_label[GPU][GPU_TEMP], "%s%d℃", module_ext_name[GPU][GPU_TEMP], info->gpu_temp);
    lv_label_set_text_fmt(module_ext_label[GPU][GPU_GRAM_USED], "%s%dMB", module_ext_name[GPU][GPU_GRAM_USED], info->gpu_ram_used);
    lv_label_set_text_fmt(module_ext_label[GPU][GPU_POWER], "%s%d.%dW", module_ext_name[GPU][GPU_POWER], info->gpu_power / 10, info->gpu_power % 10);
    lv_label_set_text_fmt(module_ext_label[GPU][GPU_FAN_SPEED], "%s%dRPM", module_ext_name[GPU][GPU_FAN_SPEED], info->gpu_fan_speed);

    //设置RAM组件
    //lv_arc_set_value(module_arc[RAM], info->ram_usage);
    lv_anim_set_var(&anim, module_arc[RAM]);
    lv_anim_set_values(&anim, lv_arc_get_value(module_arc[RAM]), info->ram_usage);
    lv_anim_start(&anim);

    lv_label_set_text_fmt(module_arc_label[RAM], "%d", info->ram_usage);
    lv_label_set_text_fmt(module_ext_label[RAM][RAM_USED], "%s%dMB", module_ext_name[RAM][RAM_USED], info->ram_used);

    //设置系统组件
    lv_label_set_text_fmt(module_ext_label[SYS][SYS_DISK], "%s\n温度 %d℃  使用率 %d%%", module_ext_name[SYS][SYS_DISK], info->disk_temp, info->disk_usage);
    lv_label_set_text_fmt(module_ext_label[SYS][SYS_NET_SPEED], "%s\n%d.%dKB/s\n%d.%dKB/s", 
        module_ext_name[SYS][SYS_NET_SPEED], info->net_download_speed / 10, info->net_download_speed % 10, 
        info->net_upload_speed /10, info->net_upload_speed % 10);
    
    //设置其他组件
  char time[6] = {"00:00"};

  time[0] += info->hour / 10;
  time[1] += info->hour % 10;
  time[3] += info->min / 10;
  time[4] += info->min % 10;


    lv_label_set_text_fmt(module_ext_label[OTHERS][OTHERS_DATE], "%s%d-%d-%d", 
        module_ext_name[OTHERS][OTHERS_DATE], info->year,info->month, info->date);
    lv_label_set_text_fmt(module_ext_label[OTHERS][OTHERS_TIME], "%s%s", 
        module_ext_name[OTHERS][OTHERS_TIME], time);
}

/*
  *@brief 默认主题释放函数
  */
static void pc_resource_gui_default_theme_release(void)
{
    lv_obj_del(background);
}

/*
 *@brief 默认主题icon点击回调函数
 */
static void pc_resource_gui_default_theme_icon_event_cb(lv_event_t *e)
{
  if (gui_opts != NULL && gui_opts->set_next_theme != NULL)
    gui_opts->set_next_theme();
}